package com.ruoyi.web.controller.demo.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;

public class Test {

    public static void main(String[] args) {

    }


    private static final AtomicLong dailyCounter = new AtomicLong(0);
    private static String currentDate = DateUtil.formatDate(new Date());

    private synchronized static String generateDailySequence() {
        String today = DateUtil.formatDate(new Date());
        if (!today.equals(currentDate)) {
            currentDate = today;
            dailyCounter.set(0);
        }
        return currentDate.replace("-", "") +
                String.format("%04d", dailyCounter.incrementAndGet() % 10000);
    }

    /**
     * 使用国家法定节假日API判断
     * API文档：https://timor.tech/api/holiday
     */
    private static boolean isHoliday(Date date) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf.format(date);

        String url = "https://timor.tech/api/holiday/info/" + dateStr;
        // 添加空指针防护和类型转换检查
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(url);

        try (CloseableHttpResponse response = httpClient.execute(httpget)) {
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200) {

                throw new Exception("节假日服务不可用");
            }

            HttpEntity entity = response.getEntity();
            if (entity == null) {

                throw new Exception("节假日服务不可用");
            }

            String responseBody = EntityUtils.toString(entity);
            JSONObject jsonResponse = JSONObject.parseObject(responseBody);

        } catch (IOException e) {

            throw new Exception("节假日服务不可用");
        }
        return false;
    }


}